/**
 * Copyright 2007 Luke Pillow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orsomethinglikethat.hinge.apt;

import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.type.PrimitiveType;

/**
 * @author lpillow
 */
public enum AccessorPrefix
{
    GET( "get" ), //
    SET( "set" ), //
    IS( "is" );

    private String prefixValue;

    private AccessorPrefix( String prefixValue )
    {
        this.prefixValue = prefixValue;
    }

    @Override
    public String toString()
    {
        return this.prefixValue;
    }

    public Boolean matchesPrefix( MethodDeclaration method, FieldDeclaration field )
    {
        Boolean result = Boolean.FALSE;

        if ( method.getSimpleName().startsWith( this.prefixValue ) )
        {
            result = this.methodToFieldName( method ).equals( field.getSimpleName() );
        }

        return result;
    }

    private String methodToFieldName( MethodDeclaration method )
    {
        String noPrefix = method.getSimpleName().substring( this.prefixValue.length() + 1, method.getSimpleName().length() );
        return String.valueOf( method.getSimpleName().charAt( this.prefixValue.length() ) ).toLowerCase() + noPrefix;
    }

    public static AccessorPrefix getterFromField( FieldDeclaration field )
    {
        if ( field.getType() instanceof PrimitiveType && ( (PrimitiveType) field.getType() ).getKind().equals( PrimitiveType.Kind.BOOLEAN ) )
        {
            return AccessorPrefix.IS;
        }
        else
        {
            return AccessorPrefix.GET;
        }
    }
}
